function getSinMul100(deg){
	var sin=[0,1,3,5,6,8,10,12,13,15,17,19,20,
			 22,24,25,27,29,30,32,34,35,37,39,
			 40,42,43,45,46,48,49,51,52,54,55,
			 57,58,60,61,62,64,65,66,68,69,70,
			 71,73,74,75,76,77,78,79,80,81,82,
			 83,84,85,86,87,88,89,89,90,91,92,
			 92,93,93,94,95,95,96,96,97,97,97,
			 98,98,98,99,99,99,99,99,99,99,99,
			 100];
	deg=(deg%360+360)%360; //deg in [0,359]
	if(deg<=90){
		return sin[deg];
	};
	if(deg<=180){
		return sin[180-deg];
	};
	if(deg<=270){
		return -sin[deg-180];
	};
	return -sin[360-deg];
};
function getCosMul100(deg){
	deg+=90;
	return getSinMul100(deg);
};


var clientGameController = Class.extend({
	ready: null,
	loading: null,
	audio: null,
	ping: null,
	server: null,
	palka1: null,
	palka2: null,
	timer : null,
	ball : null,
	world: null,
	width: 640,
	height: 400,
	tick: 0,
	len1: 200,
	len2: 200,
	startTime: null,
	dTime: 0,
	moveControll: null,
	stop: function(){
		clearInterval(this.timer);
		$('#stopButton').css({
			'display': 'block'
		});
	},
	init : function(server){
		this.tick=0;
		this.dTime=0;
		$('#startButton').css({
			display:'block'
		});
		this.audio=[new  JW.Audio.Track({
			mp3 : "/sounds/wall.mp3",
			ogg : "/sounds/wall.ogg"
		}),new  JW.Audio.Track({
			mp3 : "/sounds/wall.mp3",
			ogg : "/sounds/wall.ogg"
		}),new  JW.Audio.Track({
			mp3 : "/sounds/score.mp3",
			ogg : "/sounds/score.ogg"
		})];
		this.moveControll=[-1,-1,-1,-1,-1,-1]
		this.server=server;
		this.ball=new Ball(this.width/2, this.height/2-20);
		this.palka1=new Palka(10, this.height/2-this.len1/2, this.len1);
		this.palka2=new Palka(this.width-10, this.height/2-this.len2/2, this.len2);
		this.world=new World(this.ball,this.palka1,this.palka2);
		this.server.on("message", this.onMsg.inScope(this));
		this.ready=new ReadyView();
		this.world.elem.append(this.ready.elem1);
		this.world.elem.append(this.ready.elem2);
	},
	start: function(startSpeed, deg){
		this.ready.destroy();
		delete this.ready;
		this.ready=null;
		this.ball.speed=startSpeed;
		this.ball.deg=deg;
		this.startTime=new Date;
		this.timer=setInterval(this.onTimer.inScope(this),40);
		$('body').keydown(this.onKeyDown.inScope(this));
		$('body').keyup(this.onKeyUp.inScope(this));
	},
	onKeyDown:function(event){
		var key = event.keyCode;
        if (key == 80)
			this.server.json.send({'event': 'moveUp'});
        if (key == 76)
			this.server.json.send({'event': 'moveDown'});
	},
	onKeyUp:function(event){
		var key = event.keyCode;
        if (key == 80||key == 76)
			this.server.json.send({'event': 'stopMove'});
	},
	onMsg: function(msg){
		if(msg.event=='opponentDisconnect'){
			$('#ping').html('opponentDisconnect');
			return;
		};
		if(msg.event=='ready'){
			this.ready.ready(msg.player);
			return;
		};
		if(msg.event=='win'){
			if(msg.player){
				$('#ping').html('You win!');
			}else{
				$('#ping').html('You lose!');
			};
			return;
		};
		if(msg.event=='stop'){
			$('#ping').html('stop');
			this.stop();
			return;
		};
		if(msg.event=='ping'){
			if(this.ping){
				this.ping=(9*this.ping+(new Date-msg.time))/10;
			}else{
				this.ping=(new Date-msg.time);
			};
			$('#ping').html('ping= '+(this.ping-this.ping%1)+', connections: '+msg.connections);
			return;
		};
		if(msg.event=='moveUp1'){
			if((msg.tick-this.tick)<0){
				this.server.json.send({'event': 'synchronizationError',
										'different': (msg.tick-this.tick)});
				if(!this.loading){
					this.loading=new Loading();
					this.world.elem.append(this.loading.elem);
				};
			}else{
				this.moveControll[0]=msg.tick;
			};
			return;
		};
		if(msg.event=='moveUp2'){
			if((msg.tick-this.tick)<0){
				this.server.json.send({'event': 'synchronizationError',
										'different': (msg.tick-this.tick)});
				if(!this.loading){
					this.loading=new Loading();
					this.world.elem.append(this.loading.elem);
				};
			}else{
				this.moveControll[3]=msg.tick;
			};
			return;
		};
		if(msg.event=='moveDown1'){
			if((msg.tick-this.tick)<0){
				this.server.json.send({'event': 'synchronizationError',
										'different': (msg.tick-this.tick)});
				if(!this.loading){
					this.loading=new Loading();
					this.world.elem.append(this.loading.elem);
				};
			}else{
				this.moveControll[1]=msg.tick;
			};
			return;
		};
		if(msg.event=='moveDown2'){
			if((msg.tick-this.tick)<0){
				this.server.json.send({'event': 'synchronizationError',
										'different': (msg.tick-this.tick)});
				if(!this.loading){
					this.loading=new Loading();
					this.world.elem.append(this.loading.elem);
				};
			}else{
				this.moveControll[4]=msg.tick;
			};
			return;
		};
		if(msg.event=='stopMove1'){
			if((msg.tick-this.tick)<0){
				this.server.json.send({'event': 'synchronizationError',
										'different': (msg.tick-this.tick)});
				if(!this.loading){
					this.loading=new Loading();
					this.world.elem.append(this.loading.elem);
				};
			}else{
				this.moveControll[2]=msg.tick;
			};
			return;
		};
		if(msg.event=='stopMove2'){
			if((msg.tick-this.tick)<0){
				this.server.json.send({'event': 'synchronizationError',
										'different': (msg.tick-this.tick)});
				if(!this.loading){
					this.loading=new Loading();
					this.world.elem.append(this.loading.elem);
				};
			}else{
				this.moveControll[5]=msg.tick;
			};
			return;
		};
		if(msg.event=='scoreUpDate'){
			if(msg.player){
				this.world.score.x+=1;
			}else{
				this.world.score.y+=1;
			};
			this.world.score.upDate();
			return;
		};
		if(msg.event=='update'){
			this.update(msg.data);
			return;
		};
		if(msg.event=='start'){
			this.start(msg.startSpeed, msg.deg);
			return;
		};
	},
	moveControllProcess : function(){
		if(this.tick==this.moveControll[0]){
			this.palka1.speed=-15;
		};
		if(this.tick==this.moveControll[3]){
			this.palka2.speed=-15;
		};
		if(this.tick==this.moveControll[1]){
			this.palka1.speed=15;
		};
		if(this.tick==this.moveControll[4]){
			this.palka2.speed=15;
		};
		if(this.tick==this.moveControll[2]){
			this.palka1.speed=0;
		};
		if(this.tick==this.moveControll[5]){
			this.palka2.speed=0;
		};
	},
	onTimer : function(){
		this.tick+=1;
		if(!(this.tick%50)){
			this.server.json.send({'event': 'ping', 'time': (new Date)/1});
		};
		this.world.upDate();
		this.moveControllProcess();
		this.palka1.upDate(0,this.height-this.palka1.len);
		this.palka2.upDate(0,this.height-this.palka2.len);
		var x=this.ball.x;
		var y=this.ball.y;
		this.ball.x+=(this.ball.speed*getCosMul100(this.ball.deg)-
			this.ball.speed*getCosMul100(this.ball.deg)%100)/100;
		this.ball.y+=(this.ball.speed*getSinMul100(this.ball.deg)-
			this.ball.speed*getSinMul100(this.ball.deg)%100)/100;
		var point=null;
		if(this.ball.y<0){
			this.ball.deg=-this.ball.deg;
			this.ball.y=-this.ball.y;
			//this.audio[0].play();
		}
		if(this.ball.y>this.height){
			this.ball.deg=-this.ball.deg;
			this.ball.y=2*this.height-this.ball.y;
			//this.audio[0].play();
		}
		if(x>this.ball.x){
			point=segmentsIntersection(x,y,this.ball.x,this.ball.y,
				this.palka1.x+(this.ball.r+10),this.palka1.y,this.palka1.x+
				(this.ball.r+10),this.palka1.y+this.palka1.len);
		
			if(point){
				this.ball.deg=180-this.ball.deg;
				this.ball.x=2*(this.palka1.x+(this.ball.r+10))-this.ball.x;
				var k=point[1]-this.palka1.y;
				var deg=-(((2*k-this.palka1.len)/this.palka1.len)*30);
				deg-=deg%1;
				this.ball.deg-=deg;
				this.ball.deg=(this.ball.deg%360+360)%360;
				if(this.ball.deg>60&&this.ball.deg<=90){
					this.ball.deg=60;
				}else{
					if(this.ball.deg<300&&this.ball.deg>=270){
						this.ball.deg=300;
					};
				};
				this.world.palka1View.div.css({
					'background-image': 'url(/palkaBoom.jpg)'
				});
				this.world.palka1View.WaitTime=3;
				//this.audio[1].play();
			}
		}
		if(x<this.ball.x){
			point=segmentsIntersection(x,y,this.ball.x,this.ball.y,
				this.palka2.x-(this.ball.r+10),this.palka2.y,this.palka2.x-
				(this.ball.r+10),this.palka2.y+this.palka1.len);
		
			if(point){
				this.ball.deg=180-this.ball.deg;
				this.ball.x=2*(this.palka2.x-(this.ball.r+10))-this.ball.x;
				var k=point[1]-this.palka2.y;
				var deg=-(((2*k-this.palka2.len)/this.palka2.len)*30);
				deg-=deg%1;
				this.ball.deg+=deg;
				this.ball.deg-=180;
				this.ball.deg=(this.ball.deg%360+360)%360;
				if(this.ball.deg>60&&this.ball.deg<=90){
					this.ball.deg=60;
				}else{
					if(this.ball.deg<300&&this.ball.deg>=270){
						this.ball.deg=300;
					};
				};
				this.ball.deg-=180;
				this.world.palka2View.div.css({
					'background-image': 'url(/palkaBoom.jpg)'
				});
				this.world.palka2View.WaitTime=3;
				//this.audio[1].play();
			}
		}
		if(this.ball.x<0){
			this.ball.deg=180-this.ball.deg;
			this.ball.x=-this.ball.x;
			//this.audio[2].play();
		}
		if(this.ball.x>this.width){
			this.ball.deg=180-this.ball.deg;
			this.ball.x=this.width*2-this.ball.x;
			//this.audio[2].play();
		}
		this.dTime=new Date-this.tick*40-this.startTime;
		if(this.dTime>40){
			this.onTimer();
		}else{
			if(this.dTime<-40){
				return;
			};
		};
	},
	update: function(data){
	// this.tick=data[0];
	// ballParse(this.ball,data[1]);
	// palkaParse(this.palka1,data[2]);
	// palkaParse(this.palka2,data[3]);
	// this.moveControll=data[4];
	// this.score=data[5];
	console.log(data);
	if(this.loading){
		this.loading.destroy();
		delete this.loading;
		this.loading=null;
	};
	}
});